package com.itc.bi.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.itc.bi.config.ConnectionClickHouseConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.utils
 * @Author: hecaiy
 * @CreateTime: 2024-08-01  16:54
 * @Description: TODO  clickhouse  连接池
 * @Version: 1.0
 */
@Slf4j
public class DruidUtils {
    private static ConnectionClickHouseConfig connectionClickHouseConfig;
    public static DataSource dataSource;

    /**
     *
     * @param connectionClickHouseConfig
     */
    public static void init(ConnectionClickHouseConfig connectionClickHouseConfig) {
        Properties p = new Properties();
        String vq = "SELECT 1";
        p.put("driverClassName", connectionClickHouseConfig.getDriverClassName());
        p.put("url", connectionClickHouseConfig.getUrl());
        p.put("username", connectionClickHouseConfig.getUsername());
        p.put("password", connectionClickHouseConfig.getPassword());
        p.put("initialSize", connectionClickHouseConfig.getInitialSize());
        p.put("maxActive", connectionClickHouseConfig.getMaxActive());
        p.put("maxWait", connectionClickHouseConfig.getMaxWait());
        p.put("validationQuery", vq);
        p.put("druid.keepAlive", "true");
        p.put("init", "true");
        try {
            dataSource = DruidDataSourceFactory.createDataSource(p);
            Connection connection = dataSource.getConnection();
            close(connection, null, null);
            log.debug("数据仓库连接池创建成功");
        } catch (Exception e) {
            //链接池重试
            int i = 1;
            while (i<10){
                try {
                    long sleep = 6000;
                    log.info("第"+i+"次重试连接数据仓库连接池");
                    sleep = sleep*i;
                    Thread.sleep(sleep);
                    dataSource = DruidDataSourceFactory.createDataSource(p);
                    Connection connection = dataSource.getConnection();
                    close(connection, null, null);
                    break;
                } catch (Exception ex) {
                    log.error("第"+i+"次重试连接数据仓库连接池失败");
                }
                i++;
            }
        }
    }

    /**
     * 获取数据库连接池对象的方法
     * @return
     */
    public DataSource getDataSource() {
        return dataSource;
    }

    // 获取数据库连接对象的方法
    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            log.info("连接成功");
        }
    }

    // 归还数据库连接对象的方法
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            // 归还数据库连接对象
            if (conn != null) conn.close();
            // 释放sql语句执行对象
            if (ps != null) ps.close();
            // 释放查询结果集
            if (rs != null) rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
